<?php

namespace App\Scopes;

use App\Models\NewUsers;
use App\Models\SaleAccount;
use App\Models\Warehouse;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class WarehouseScope implements Scope
{
    /**
     * 用户权限，用户有权限的仓库，相对应控制器加上全局作用域
     * 把约束加到 Eloquent 查询构造中
     *
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        if (Auth::check() && !Admin::user()->isAdministrator() && !NewUsers::isManager() && !NewUsers::isManagerGe()) {

            $arr = getLoginUserWarehouse(Admin::user()->id);
            $builder->whereIn($model->getTable() . '.sid', $arr);
        }

    }
}